package com.szcatic.ework.controller.auth;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.szcatic.ework.service.auth.PermissionService;
import com.szcatic.ework.vo.User;
import com.szcatic.ework.vo.auth.Permission;

/**
 * 权限管理Controller
 * @author Administrator
 *
 */
@Controller
@RequestMapping("auth/permission")
public class PermissionController {
	
	private Logger log = Logger.getLogger(PermissionController.class);
	
	@Autowired
	private PermissionService permissionService;
	
	@Autowired
	private HttpServletRequest request;
	
	@RequestMapping("")
	@RequiresPermissions(value={"auth:permission:select"})
	public String index() {
		return "auth/permission";
	}
	
	@RequestMapping("queryPermissionList")
	@RequiresPermissions(value={"auth:permission:select"})
	@ResponseBody
	public Map<String,Object> queryPermissionList() {
		PageHelper.startPage(Integer.valueOf(request.getParameter("page")), Integer.valueOf(request.getParameter("limit")));
		List<Permission> userList= permissionService.queryPermissionList();
		PageInfo<Permission> pageInfo=new PageInfo<Permission>(userList);
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("count", pageInfo.getTotal());
		map.put("data", pageInfo.getList());
		map.put("code", "0"); //成功的状态码
		map.put("msg", "");
		return map;
	}
	
	/**
	 * 更新权限
	 * @return
	 */
	@RequestMapping("update")
	@RequiresPermissions(value={"auth:permission:update"})
	@ResponseBody
	public String updatePermission(@RequestBody Permission permission) {
		try {
			User user = (User)request.getSession().getAttribute("user");
			permission.setUpdateUser(user);
			permissionService.update(permission);
			return "1";
		} catch(Exception e) {
			return "-1";
		}
	}
	
	/**
	 * 新增权限
	 * @return
	 */
	@RequestMapping("add")
	@RequiresPermissions(value={"auth:permission:add"})
	@ResponseBody
	public String addPermission(@RequestBody Permission permission) {
		try {
			User user = (User)SecurityUtils.getSubject().getSession().getAttribute("user");
			permission.setCreateUser(user);
			permission.setUpdateUser(user);
			permissionService.add(permission);
			return "1";
		} catch (Exception e) {
			log.error("新增权限失败",e);
			return "-1";
		}
		
	}
	
	/**
	 * 保存权限
	 * @return
	 */
	@RequestMapping("save")
	@RequiresPermissions(value={"auth:permission:save"})
	@ResponseBody
	public String savePermission(@RequestBody Permission permission) {
		try {
			User user = (User)SecurityUtils.getSubject().getSession().getAttribute("user");
			permission.setUpdateUser(user);
			if (StringUtils.isBlank(permission.getId())) {
				permission.setCreateUser(user);
				permissionService.add(permission);
			} else {
				permissionService.update(permission);
			}
			return "1";
		} catch (Exception e) {
			log.error("保存权限失败",e);
			return "-1";
		}
		
	}
	
	/**
	 * 
	 * @return
	 */
	@RequestMapping("check")
	@RequiresPermissions(value={"auth:permission:save"})
	@ResponseBody
	public String checkPermissionExist(@RequestBody Permission permission) {
		try {
			int flag = permissionService.checkPermissionExist(permission);
			if (flag >= 1) {
				//存在
				return "-1";
			} else {
				//不存在
				return "1";
			}
		} catch(Exception e) {
			log.error("验证权限失败", e);
			//验证失败
			return "-2";
		}
	}
	
	/**
	 * 删除权限
	 * @return
	 */
	@RequestMapping("delete")
	@RequiresPermissions(value={"auth:permission:delete"})
	@ResponseBody
	public String deletePermission(@RequestBody List<String> idList) {
		try {
				permissionService.deleteList(idList);
				return "1";
		} catch(Exception e) {
			log.error("删除权限失败", e);
			//删除失败
			return "-2";
		}
	}
	
}
